/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.serverpackets;

import java.util.concurrent.atomic.AtomicInteger;

import l1j.server.server.model.L1Character;

// Referenced classes of package l1j.server.server.serverpackets:
// ServerBasePacket

public class S_UseArrowSkill extends ServerBasePacket {

	private static final String S_USE_ARROW_SKILL = "[S] S_UseArrowSkill";
	private static AtomicInteger _sequentialNumber = new AtomicInteger(0);

	private byte[] _byte = null;

	/**
	 * 物件攻擊 - <font color="#ff0000">未命中</font>(遠程-物理攻擊 PC/NPC共用) 空攻擊使用
	 * 
	 * @param cha
	 *            執行者
	 * @param spellgfx
	 *            遠程動畫編號
	 * @param x
	 *            目標X
	 * @param y
	 *            目標Y
	 */
	public S_UseArrowSkill(final L1Character cha, final int spellgfx,
			final int x, final int y) {

		int aid = 1;
		// 外型編號改變動作
		if (cha.getTempCharGfx() == 3860) {
			aid = 21;
		}
		this.writeC(S_OPCODE_ATTACKPACKET);
		this.writeC(aid);// 動作代號
		this.writeD(cha.getId());// 執行者OBJID
		this.writeD(0x00);// 目標OBJID
		this.writeH(0x00);// 傷害力
		this.writeC(cha.getHeading());// 新面向

		// 以原子方式将当前值加 1。
		this.writeD(0x00000152);

		this.writeH(spellgfx);// 遠程動畫編號
		this.writeC(0x7f);// ??
		this.writeH(cha.getX());// 執行者X點
		this.writeH(cha.getY());// 執行者Y點
		this.writeH(x);// 目標X點
		this.writeH(y);// 目標Y點

		this.writeD(0x00000000);
		this.writeC(0x00);
		// this.writeC(0x00);
		// this.writeC(0x00);
		// this.writeC(0x00);
	}

	public S_UseArrowSkill(final L1Character cha, final int targetobj,
			final int spellgfx, final int x, final int y, final boolean isHit) {

		int aid = 1;
		// オークアーチャーのみ変更
		if (cha.getTempCharGfx() == 3860) {
			aid = 21;
		}
		this.writeC(S_OPCODE_ATTACKPACKET);
		this.writeC(aid);
		this.writeD(cha.getId());
		this.writeD(targetobj);
		this.writeH(isHit ? 6 : 0);// 修正妖精弓箭斷線問題by0968026609
		this.writeC(cha.getHeading());
		// writeD(0x12000000);
		// writeD(246);
		this.writeD(_sequentialNumber.incrementAndGet());
		this.writeH(spellgfx);
		this.writeC(127); // スキル使用時の光源の広さ？
		this.writeH(cha.getX());
		this.writeH(cha.getY());
		this.writeH(x);
		this.writeH(y);
		// writeC(228);
		// writeC(231);
		// writeC(95);
		// writeC(82);
		// writeC(170);
		this.writeC(0);
		this.writeC(0);
		this.writeC(0);
		this.writeC(0);
		this.writeC(0);
	}

	/**
	 * 物件攻擊 - <font color="#ff0000">命中</font>(遠程-物理攻擊 PC/NPC共用)
	 * 
	 * @param cha
	 *            執行者
	 * @param targetobj
	 *            目標OBJID
	 * @param spellgfx
	 *            遠程動畫編號
	 * @param x
	 *            目標X
	 * @param y
	 *            目標Y
	 * @param dmg
	 *            傷害力
	 */
	public S_UseArrowSkill(final L1Character cha, final int targetobj,
			final int spellgfx, final int x, final int y, final int dmg) {

		int aid = 1;
		// 外型編號改變動作
		switch (cha.getTempCharGfx()) {
		case 3860:// 妖魔弓箭手
			aid = 21;
			break;

		case 2716:// 古代亡靈
			aid = 19;
			break;
		}

		/*
		 * 0000: 5e 01 8e 24 bb 01 a4 6c 00 00 0a 00 05 52 01 00
		 * ^..$...l.....R.. 0010: 00 42 00 00 c3 83 e1 7e c1 83 e5 7e 00 00 00
		 * 85 .B.....~...~....
		 * 
		 * 0000: 5e 01 8e 24 bb 01 a4 6c 00 00 0d 00 05 52 01 00
		 * ^..$...l.....R.. 0010: 00 42 00 00 c3 83 e1 7e c1 83 e5 7e 00 00 00
		 * ee .B.....~...~....
		 * 
		 * 0000: 5e 01 8e 24 bb 01 3c 20 00 00 0b 00 05 52 01 00 ^..$..<
		 * .....R.. 0010: 00 42 00 00 c3 83 e1 7e c0 83 e5 7e 00 00 00 58
		 * .B.....~...~...X
		 */
		this.writeC(S_OPCODE_ATTACKPACKET);
		this.writeC(aid);// 動作代號
		this.writeD(cha.getId());// 執行者OBJID
		this.writeD(targetobj);// 目標OBJID

		if (dmg > 0) {
			this.writeH(0x0a); // 傷害值

		} else {
			this.writeH(0x00); // 傷害值
		}

		this.writeC(cha.getHeading());// 新面向

		// 以原子方式将当前值加 1。
		this.writeD(0x00000152);

		this.writeH(spellgfx);// 遠程動畫編號
		this.writeC(0x7f);// ??
		this.writeH(cha.getX());// 執行者X點
		this.writeH(cha.getY());// 執行者Y點
		this.writeH(x);// 目標X點
		this.writeH(y);// 目標Y點

		this.writeD(0x00000000);
		this.writeC(0x00);
		// this.writeC(0x00);
		// this.writeC(0x00);
		// this.writeC(0x00);
	}

	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this._bao.toByteArray();
		} else {
			// TODO 修正無預警斷線問題byl1j-tw-99nets開發團隊
			final int seq = _sequentialNumber.incrementAndGet();
			this._byte[13] = (byte) (seq & 0xff);
			this._byte[14] = (byte) (seq >> 8 & 0xff);
			this._byte[15] = (byte) (seq >> 16 & 0xff);
			this._byte[16] = (byte) (seq >> 24 & 0xff);
			// TODO 修正無預警斷線問題byl1j-tw-99nets開發團隊

		}
		return this._byte;
	}

	@Override
	public String getType() {
		return S_USE_ARROW_SKILL;
	}

}
